<?php
/*
 * @Description: 
 * @Author: 橘子味的心
 * @Date: 2020-11-13 15:02:02
 * @LastEditTime: 2020-11-13 15:27:25
 * @LastEditors: 橘子味的心
 * @Reference: 
 */
namespace x_mier\neditor\util;

/**
 * 扩展安装更新及卸载的资源文件操作
 * 1. 安装更新时,复制assets/neditor文件到/public/static/plug/neditor/
 * 2. 卸载时,删除/public/static/plug/neditor/,如果/public/static/plug为空也一并删除了
 */
class Recource
{
    /**
     * 安装动作
     *
     * @return boolean
     */
    static public function install($overWrite = true)
    {   
        $assets = __DIR__.'/../../assets/neditor/';
        $static = root_path().'/public/static/plug/neditor/';
        $res = FileUtil::copyDir($assets,$static,$overWrite);

        if(!$res){
            throw new \Exception('资源安装失败');
        }
        return $res;
    }

    /**
     * 卸载操作
     *
     * @return boolean
     */
    static public function uninstall()
    {
        $parentDir = root_path().'/public/static/plug/';
        $ueditorDir = $parentDir.'neditor/';
        $res = FileUtil::unlinkDir($ueditorDir);
        if($res){
            if(FileUtil::isDirEmpty($parentDir)){
                $res = FileUtil::unlinkDir($parentDir);
            }
        }
        if(!$res){
            throw new \Exception('资源删除失败,请手动删除'.$ueditorDir);
        }
        return $res;
    }
}
